package com.bosssoft.hr.jobmonitor.controller;

import com.bosssoft.hr.jobmonitor.config.common.SysConfig;
import com.bosssoft.hr.jobmonitor.core.file.FileTool;
import com.bosssoft.hr.jobmonitor.core.job.TaskTool;
import com.bosssoft.hr.jobmonitor.entity.Auth;
import com.bosssoft.hr.jobmonitor.entity.Task;

import com.bosssoft.hr.jobmonitor.util.DateUtil;
import com.bosssoft.hr.jobmonitor.util.LoggerUtil;
import com.bosssoft.hr.jobmonitor.util.SchedulerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/***
 * @Author lys(cherish)
 * @Description 任务控制
 * @Date  2019/2/26
 **/
@RestController
@RequestMapping("/task")
public class TaskController {


    private static final Logger logger= LoggerFactory.getLogger(TaskController.class);

    //任务处理工具注入
    @Autowired
    private TaskTool taskTool;

    //文件处理工具注入
    @Autowired
    private FileTool fileTool;

    //定时工具注入
    @Autowired
    private SchedulerUtil schedulerUtill;

    /***
     * @Author lys(cherish)
     * @Description 添加一个任务
     * @Date  2019/2/26
     * @Param [task, session]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @PostMapping("/task")
    public ModelAndView saveTask(@ModelAttribute(value = "task")  Task task, HttpSession session){
        logger.info(LoggerUtil.fmtLog("管理员发起任务",task));
        if(task==null){
            logger.info(LoggerUtil.fmtLog("任务不完整,无效",task));
            return null;
        }
        task.setId(taskTool.getLastTask().getId()+1);
        String dateStr = DateUtil.fmtDate2Str(new Date());
        task.setReleasetime(dateStr);
        taskTool.saveTask(task);
        doMailTimer(task,task.getRemind().getEndtime());
        ModelAndView modelAndView = new ModelAndView("redirect:/task/list");
        return modelAndView;
    }

    /***
     * @Author lys(cherish)
     * @Description 计算时间并开启定时监控
     * @Date  2019/2/26
     * @Param [task, endTimeStr]
     * @return void
     **/
    public void doMailTimer(Task task,String endTimeStr){
        logger.info(LoggerUtil.fmtLog("开启定时任务",task));
        Date sendTime=null;
        //管理员没有指定提醒时间，默认在截止时间前一小时
        if("".equals(endTimeStr)||endTimeStr==null){
            SysConfig sysconfig = fileTool.getSysConfig();
            String preMailTime = sysconfig.getPreMailTime();
            Integer preMinute = Integer.valueOf(preMailTime);
            Date date = DateUtil.fmtStr2Date(endTimeStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MINUTE,-1*preMinute);
            sendTime = cal.getTime();
        }
        sendTime = DateUtil.fmtStr2Date(endTimeStr);
        // 创建文件夹
        fileTool.makeHomeworkDir();
        // 开启定时任务
        schedulerUtill.startCron(task,sendTime);
    }

    /***
     * @Author lys(cherish)
     * @Description 获取所有任务列表
     * @Date  2019/2/26
     * @Param []
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @GetMapping("list")
    public ModelAndView getAllTask(){
        logger.info(LoggerUtil.fmtLog("查询所有任务"));
        List<Task> allTask = taskTool.getAllTask();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("task");
        modelAndView.getModel().put("taskList",allTask);
        return modelAndView;
    }

}
